#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib> // 用于调用 gnuplot

// 截断幂函数
double pow_trunc(double x, double t, int n) {
    if (t - x > 0) {
        return pow((t - x), n);
    } else {
        return 0;
    }
}

// 差商计算
std::vector<double> diff(double x, const std::vector<double>& t, int n) {
    int size = t.size();
    std::vector<double> result(size);

    for (int i = 0; i < size - 1; i++) {
        result[i] = (pow_trunc(x, t[i+1], n) - pow_trunc(x, t[i], n)) / (t[i + 1] - t[i]);
    }
    result[size - 1] = (result[1] - result[0]) / (t[2] - t[0]); // 二阶差商结果

    return result;
}

int main() {
    // 定义插值节点向量
    std::vector<double> t = {1, 2, 3};  

    // 遍历 n = 1 和 n = 2 的情况
    for (int n = 1; n <= 2; n++) {
        // 文件输出流
        std::string data_filename = "difference_quotients_n" + std::to_string(n) + ".dat";
        std::ofstream data_file(data_filename);

        // 保存数据
        std::vector<double> x_vals, pow_0_1_vals, pow_0_2_vals, pow_0_3_vals;
        std::vector<double> diff_1_vals, diff_2_vals, diff_3_vals;

        // 计算 x 从 0 到 3 的差商值
        for (double x = 0; x <= 3; x += 0.1) {
            std::vector<double> result = diff(x, t, n);

            // 保存截断幂函数和差商值
            x_vals.push_back(x);
            pow_0_1_vals.push_back(pow_trunc(x, t[0], n));
            pow_0_2_vals.push_back(pow_trunc(x, t[1], n));
            pow_0_3_vals.push_back(pow_trunc(x, t[2], n));
            diff_1_vals.push_back(result[0]);
            diff_2_vals.push_back(result[1]);
            diff_3_vals.push_back(result[2]);

            // 写入数据文件
            data_file << x << " "
                      << pow_trunc(x, t[0], n) << " "
                      << pow_trunc(x, t[1], n) << " "
                      << pow_trunc(x, t[2], n) << " "
                      << result[0] << " "
                      << result[1] << " "
                      << result[2] << "\n";
        }

        data_file.close();

        // 自动生成 Gnuplot 绘图脚本
        // 绘制截断幂函数
        std::string plot_filename_1 = "pow_trunc_n" + std::to_string(n) + ".plt";
        std::ofstream plot_file_1(plot_filename_1);

        plot_file_1 << "set terminal pngcairo enhanced font 'Arial,10' size 800,600\n";
        plot_file_1 << "set output 'pow_trunc_n" + std::to_string(n) + ".png'\n";
        plot_file_1 << "set title 'Truncated Power Functions (n=" + std::to_string(n) + ")'\n";
        plot_file_1 << "set xlabel 'x'\n";
        plot_file_1 << "set ylabel 'Values'\n";
        plot_file_1 << "set grid\n";
        plot_file_1 << "plot \\\n";
        plot_file_1 << "    '" + data_filename + "' using 1:2 with lines title 'pow_trunc (t[0], n=" + std::to_string(n) + ")' lc rgb 'red', \\\n";
        plot_file_1 << "    '" + data_filename + "' using 1:3 with lines title 'pow_trunc (t[1], n=" + std::to_string(n) + ")' lc rgb 'green', \\\n";
        plot_file_1 << "    '" + data_filename + "' using 1:4 with lines title 'pow_trunc (t[2], n=" + std::to_string(n) + ")' lc rgb 'blue'\n";

        plot_file_1.close();

        // 绘制一阶差商
        std::string plot_filename_2 = "diff_1_n" + std::to_string(n) + ".plt";
        std::ofstream plot_file_2(plot_filename_2);

        plot_file_2 << "set terminal pngcairo enhanced font 'Arial,10' size 800,600\n";
        plot_file_2 << "set output 'diff_1_n" + std::to_string(n) + ".png'\n";
        plot_file_2 << "set title 'First Difference Quotient (n=" + std::to_string(n) + ")'\n";
        plot_file_2 << "set xlabel 'x'\n";
        plot_file_2 << "set ylabel 'Values'\n";
        plot_file_2 << "set grid\n";
        plot_file_2 << "plot \\\n";
        plot_file_2 << "    '" + data_filename + "' using 1:5 with lines title 'diff_1 (n=" + std::to_string(n) + ")' lc rgb 'magenta'\n";

        plot_file_2.close();

        // 绘制二阶差商
        std::string plot_filename_3 = "diff_2_n" + std::to_string(n) + ".plt";
        std::ofstream plot_file_3(plot_filename_3);

        plot_file_3 << "set terminal pngcairo enhanced font 'Arial,10' size 800,600\n";
        plot_file_3 << "set output 'diff_2_n" + std::to_string(n) + ".png'\n";
        plot_file_3 << "set title 'Second Difference Quotient (n=" + std::to_string(n) + ")'\n";
        plot_file_3 << "set xlabel 'x'\n";
        plot_file_3 << "set ylabel 'Values'\n";
        plot_file_3 << "set grid\n";
        plot_file_3 << "plot \\\n";
        plot_file_3 << "    '" + data_filename + "' using 1:6 with lines title 'diff_2 (n=" + std::to_string(n) + ")' lc rgb 'cyan'\n";

        plot_file_3.close();

        // 绘制三阶差商
        std::string plot_filename_4 = "diff_3_n" + std::to_string(n) + ".plt";
        std::ofstream plot_file_4(plot_filename_4);

        plot_file_4 << "set terminal pngcairo enhanced font 'Arial,10' size 800,600\n";
        plot_file_4 << "set output 'diff_3_n" + std::to_string(n) + ".png'\n";
        plot_file_4 << "set title 'Third Difference Quotient (n=" + std::to_string(n) + ")'\n";
        plot_file_4 << "set xlabel 'x'\n";
        plot_file_4 << "set ylabel 'Values'\n";
        plot_file_4 << "set grid\n";
        plot_file_4 << "plot \\\n";
        plot_file_4 << "    '" + data_filename + "' using 1:7 with lines title 'diff_3 (n=" + std::to_string(n) + ")' lc rgb 'yellow'\n";

        plot_file_4.close();

        // 调用 gnuplot 绘制所有图像
        std::system(("gnuplot " + plot_filename_1).c_str());
        std::system(("gnuplot " + plot_filename_2).c_str());
        std::system(("gnuplot " + plot_filename_3).c_str());
        std::system(("gnuplot " + plot_filename_4).c_str());

        std::cout << "Generated plots for n=" << n << std::endl;
    }

    return 0;
}